﻿Imports OPC.Common
Imports OPC.Data.Interface
Imports OPC.Data
Imports System.Drawing.Imaging.ImageFormat
Imports System.Runtime.InteropServices
Imports System.Threading

Module Mod_OPCHander

    Public m_ModName As String = "Mod_OPCHander"
    Private ItemDefs() As OPCItemDef
    Private HandlesSrv(plcparamnum) As Integer

    '初始化OPC服务器
    Public Sub initOPCServer()
        TheSrv = New OpcServer
        TheSrv.Connect("S7200.OPCServer")
        TheGrp = TheSrv.AddGroup("", False, 900)
        TheGrp.SetEnable(True)
        TheGrp.Active = False
        plcparamnum = 35
        ReDim plcaddr(plcparamnum)
        ReDim ItemDefs(plcparamnum)
        ReDim rItm(plcparamnum)
        initLineData()
    End Sub

    Private Sub initLineData()
        Dim funName As String = "initLineData"
        Dim sqlStr = "select id,plc_param,plc_addr,addr_type,param_type,belong_lineid from t_plcparam order by id"
        Dim dt As DataTable = execSQLQuery(sqlStr)
        Dim i As Integer = 0
        For i = 0 To dt.Rows.Count - 1
            plcaddr(i) = "2," & dt.Rows(i).Item("plc_addr") & "," & dt.Rows(i).Item("addr_type") & ",RW"
        Next
        For i = 0 To plcparamnum
            ItemDefs(i) = New OPCItemDef(plcaddr(i), True, 1234, VarEnum.VT_EMPTY)
        Next
        TheGrp.AddItems(ItemDefs, rItm)
        If rItm Is Nothing Then Exit Sub
        For i = 0 To plcparamnum
            If rItm(i).Error Then
                ErrInfoHandle(m_ModName, funName, "添加Item" & i & "错误", "")
                Return
            End If
        Next
    End Sub

    
    '数据保存
    Public Sub savePicInfo(ByVal lineid As String, ByVal iserrpic As Boolean)
        'g_frmmain.BeginInvoke(New EventHandler(AddressOf Weight_Info), lineid.ToString)
        time2 = Now
        If (lineid = "1") Then
            cid = g_frmmain.CvsInSightDisplay1
        ElseIf (lineid = "2") Then
            cid = g_frmmain.CvsInSightDisplay2
        ElseIf (lineid = "3") Then
            cid = g_frmmain.CvsInSightDisplay3
        ElseIf (lineid = "4") Then
            cid = g_frmmain.CvsInSightDisplay4
        Else
            Exit Sub
        End If
        Dim lineindex As Integer = CInt(lineid) - 1
        If (cid.Connected = True) Then
            Dim strupicinfo As New struPicInfo
            strupicinfo.linecode = lineid
            strupicinfo.stackno = linedata(lineindex).stackno
            strupicinfo.stacknum = linedata(lineindex).stacknum
            strupicinfo.picvalue = cid.Results.Cells.GetCell(62, 1).Text.ToString
            setPicInfo(strupicinfo)
            'true表示是好的,false表示次品,保存到数据库里面是好的是0 坏的是1
            Dim sqlStr As String = "insert into t_PicInfo (pic_name,pic_url,line_code,stack_no,stack_num,pic_value,crt_date,is_error,classcode,usercode)values('" & strupicinfo.picname & "','" & strupicinfo.picurl & "','" & CStr(strupicinfo.linecode) & "','" & CStr(strupicinfo.stackno) & "','" & CStr(strupicinfo.stacknum) & "','" & strupicinfo.picvalue & "','" & CStr(strupicinfo.crttime) & "'," & CInt(iserrpic) & ",'" & classcode & "','" & username & "')"
            execSQLQueryNoResult(sqlStr)
            If (strupicinfo.picurl <> "") Then
                If (System.IO.Directory.Exists(strupicinfo.directory) = False) Then
                    MkDir(strupicinfo.directory)
                End If
                time3 = Now
                cid.Results.Image.Save(strupicinfo.picurl, Jpeg)
            End If
        End If
    End Sub


    '设置图片信息，name，url，目录，时间
    Private Sub setPicInfo(ByRef strudetectarget As struPicInfo)
        strudetectarget.crttime = Now
        Dim s As String = ""
        If (badprodnum = 0) Then
            s = ""
        Else
            s = CStr(badprodnum)
        End If
        strudetectarget.picname = Format(strudetectarget.crttime, "yyyyMMdd") & spacemark & CStr(strudetectarget.linecode) & spacemark & CStr(strudetectarget.stackno) & spacemark & CStr(strudetectarget.stacknum) & s & pictype
        strudetectarget.picurl = picUrlRoot + "\" + Format(strudetectarget.crttime, "yyyyMMdd") + "\" + strudetectarget.picname
        strudetectarget.directory = picUrlRoot + "\" + Format(strudetectarget.crttime, "yyyyMMdd") + "\"
    End Sub

    Public Sub closeOPCConnect()
        Dim funName As String = "closeOPCConnect"
        Dim aE(plcparamnum) As Integer
        If Not ItemDefs(0) Is Nothing Or Not ItemDefs(1) Is Nothing Then
            If Not TheGrp Is Nothing Then
                TheGrp.RemoveItems(HandlesSrv, aE)
            End If
        End If

        If Not TheGrp Is Nothing Then

            Try
                TheGrp.Remove(False)
            Catch ex As Exception
                ErrInfoHandle(m_ModName, funName, ex.Message, ex.Source)
            End Try

            TheGrp = Nothing
        End If

        If Not TheSrv Is Nothing Then

            Try
                TheSrv.Disconnect()
            Catch ex As Exception
                ErrInfoHandle(m_ModName, funName, ex.Message, ex.Source)
            End Try

            TheSrv = Nothing
        End If
    End Sub


    Public Function updatePLCDate(ByVal plcnum As Integer, ByVal plcvalue As Integer) As Boolean
        Dim funName As String = "updatePLCDate"
        Dim arrHSrv(0) As Integer
        Dim arrVal(0) As Object
        Dim arrErr(0) As Integer
        arrHSrv(0) = rItm(plcnum).HandleServer
        arrVal(0) = plcvalue
        Try
            TheGrp.SyncWrite(arrHSrv, arrVal, arrErr)
            If arrErr(0) Then
                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            ErrInfoHandle(m_ModName, funName, ex.Message, ex.Source)
        End Try
    End Function

    Public Function getPLCDate(ByVal plcnum As Integer) As Integer
        Dim funName As String = "getPLCDate"
        Dim aE(0) As Integer

        ' Sync Read
        Dim arrHSrv(0) As Integer
        Dim arrStat(0) As OPCItemState

        arrHSrv(0) = rItm(plcnum).HandleServer

        Try
            TheGrp.SyncRead(OPCDATASOURCE.OPC_DS_DEVICE, arrHSrv, arrStat)

            If arrStat(0).Quality = 192 Then
                Return arrStat(0).DataValue
            Else
                Return -1
            End If

        Catch ex As Exception
            ErrInfoHandle(m_ModName, funName, ex.Message, ex.Source)
        End Try
    End Function
End Module
